package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.Car;
import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author xilei
 * @create 2021-09-29 下午 6:52
 */
@RestController
public class CarController {

    @Autowired
    CarService carService;


    /**
     * 分页查找
     * @param model
     * @param pageIndex
     * @param pageSize
     * @param car
     * @return
     */
    @GetMapping("/carlist")
    public ResponseResult<PageBean<Car>> getAllCar(Model model, Integer pageIndex, Integer pageSize,Car car){
        List<Car> carByPage = carService.findCarByPage(pageIndex, pageSize);
        for(Car a:carByPage){
            System.out.println(a);
        }
        PageBean<Car> pageBean = new PageBean<>();
        pageBean.setTotalRecord(carService.findCar().size());
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setBeanList(carByPage);
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    /**
     * 模糊查询
     * @param model
     * @param car_name
     * @param car_type
     * @param car_vehicle
     * @param pageIndex
     * @param pageSize
     * @param car
     * @return
     */
    @GetMapping("/carlist1")
    public ResponseResult<PageBean<Car>> getAllCarBytiaojian(Model model,String car_name, String car_type, String car_vehicle, Integer pageIndex, Integer pageSize,Car car){
        List<Car> carByPageList = carService.findCarByPageByParam(car_name, car_type, car_vehicle, pageIndex, pageSize);
        PageBean<Car> pageBean = new PageBean<>();
        pageBean.setTotalRecord(carService.findCar().size());
        pageBean.setPageSize(pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setBeanList(carByPageList);
        model.addAttribute("carByPageList",carByPageList);
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    /**
     * 增车辆信息
     * @param car
     * @return
     */
    @PostMapping("/carlist")
    public ResponseResult addCar(@RequestBody Car car){
        System.out.println(car);
        carService.insertCar(car);
        return new ResponseResult(null, 200, "OK");
    }

    /**
     * 删车辆信息
     * @param car_id
     * @return
     */
    @DeleteMapping("/carlist")
    public ResponseResult deleteCar(Integer car_id){
        System.out.println(car_id);
        carService.deleteCar(car_id);
        return new ResponseResult(null, 200, "OK");
    }

    /**
     * 改车辆信息
     * @param car
     * @return
     */
    @PutMapping("/carlist")
    public ResponseResult updateCar(@RequestBody Car car ){
        System.out.println(car);
        carService.updateCar(car.getCar_name(), car.getCar_type(), car.getCar_vehicle(), car.getCar_picture(), car.getCar_id());
        return new ResponseResult(null, 200, "OK");
    }

}
